/*
	新闻详情
		新闻内容修改
*/

var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');
var dictData = Cookies.getJSON('kplus_web_dict');

// dom 
$(function(){
	layui.use(['layer', 'form', 'table'], function(){
		var layer = layui.layer;
		var form = layui.form;
		var table = layui.table;

		//ajax data
		var ajaxData = {
			aId: adminLoginedInfo.aId,
			aToken: adminLoginedInfo.aToken
		};
		// wang editor 富文本编辑器
		var WEditor = window.wangEditor,
            editorCn = new WEditor('#htmlCn'),
            editorTw = new WEditor('#htmlTw'),
            editorEn = new WEditor('#htmlEn');
        editorCn.create();
        editorTw.create();
        editorEn.create();

        // 获取新闻详情
        function news(ajaxData){
        	var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
        	var _ajaxData = $.extend({}, {wId: getParam('w_news')}, ajaxData);
        	//console.log(_ajaxData)
        	$.ajax({
        		url: BASE_SERVICEURL + URlS.news,
        		type:'GET', 
        		data: _ajaxData,
        		success: function(responseData){
        			if(responseData.msg.code == 200){
        				layer.close(loadIndex);
        				//console.log(responseData);
        				initFormData(responseData.data);
        			}else{
        				layer.close(loadIndex);
        				layer.msg(responseData.msg.msg, {time: 2000});
        			}
        		},
        		error: function(responseData){
        			layer.close(loadIndex);
        			layer.msg(MESSAGES.service_error, {time: 2000, end: function(){
        				location.href = './500.html';
        			}})
        		}
        	})
        }
        news(ajaxData);
        //处理表单数据
        function initFormData(data){
        	var form_data = {
        		titleCn: data.news_title['cn'],
        		titleTw: data.news_title['tw'],
        		titleEn: data.news_title['en'],
        		status: data.news_status,
        		tags: data.new_tags.join('|')
        	}
        	//轮播图添加
        	var new_banner_box = $('#new-banner-box')
        	for(var i = 0; i < data.new_banner.length; i++){
        		var item = DOMHTMLSTRINGS.new_banner_img(i);
        		new_banner_box.append(item);
        		form_data['newsImg'+i] = data.new_banner[i];
        	};
        	//富文本载入
        	editorCn.txt.html(data.html_cn);
            editorTw.txt.html(data.html_tw);
            editorEn.txt.html(data.html_en);
        	form.val('news-data-form', form_data);
        	form.render();
        }
        //修改信息
        function modifyNews(formData){
        	var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
        	var _ajaxData = $.extend({wId: getParam('w_news')}, formData ,ajaxData);
        	$.ajax({
        		url: BASE_SERVICEURL + URlS.news,
        		type:'POST',
        		data: _ajaxData,
        		success: function(responseData){
        			if(responseData.msg.code == 200){
        				layer.close(loadIndex);
        				layer.msg(responseData.data.msg, {time:2000, end: function(){
        					location.reload();
        				}})
        			}else{
        				layer.close(loadIndex);
        				layer.msg(responseData.msg.msg, {time: 2000});
        			}
        		},
        		error: function(responseData){
        			layer.close(loadIndex);
        			layer.msg(MESSAGES.service_error, {time: 2000, end:function(){
        				location.href = './500.html';
        			}})
        		}
        	})
        }
        function deleteNews(ajaxData){
            var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
            var _ajaxData = $.extend({}, {wId: getParam('w_news')}, ajaxData);
            //console.log(_ajaxData)
            $.ajax({
                url: BASE_SERVICEURL + URlS.news,
                type:'DELETE', 
                data: _ajaxData,
                success: function(responseData){
                    if(responseData.msg.code == 200){
                        layer.close(loadIndex);
                        layer.msg(responseData.msg.msg, {time: 2000, end: function(){
                            //关闭窗口并刷新父窗口
                            parent.location.reload();
                        }})
                    }else{
                        layer.close(loadIndex);
                        layer.msg(responseData.msg.msg, {time: 2000});
                    }
                },
                error: function(responseData){
                    layer.close(loadIndex);
                    layer.msg(MESSAGES.service_error, {time: 2000, end: function(){
                        location.href = './500.html';
                    }})
                }
            })
        }
        // 修改信息
        form.on('submit(modify)', function(data){
        	//console.log(data)
        	var form_data = {
        		cn: editorCn.txt.html(),
                tw: editorTw.txt.html(),
                en: editorEn.txt.html(),
                tags: data.field.tags,                                                                                                                              
                title: langTransObj(data.field.titleCn, data.field.titleTw, data.field.titleEn),
                status: data.field.status
        	};
        	var img_length = $('#new-banner-box input.layui-input').length;
        	var img_arr = [];
        	for(var i = 0; i < img_length; i++){
        		img_arr.push(data.field['newsImg'+i]);
        	}
        	form_data['img'] = img_arr.join('|');
        	modifyNews(form_data);
        	return false;
        })
        // delete
        form.on('submit(delete)', function(){
            layer.confirm(MESSAGES.confirm_delete, {title: '删除提示'}, function(index){
                deleteNews(ajaxData)
                layer.close(index);
            })
            return false;
        })
	});
})







